using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ACME.Northwind.DataTransfer;
using ACME.Northwind.Business.Objects;
using ACME.Northwind.Service.Interfaces;
using Widgetsphere.Core.DataAccess;
using ACME.Northwind.DALProxy.Extensions;

namespace ACME.Northwind.DALProxy
{
	/// <summary>
	/// The service implementation for Customer object
	/// </summary>
	public partial class CustomerService : ICustomerService
	{
		#region ICustomerService Members

		/// <summary>
		/// Run a selection by the primary key
		/// </summary>
		public CustomerDTO SelectByPrimaryKey(string customerid)
		{
			CustomerDTO retVal = new CustomerDTO();
			if (!retVal.SelectUsingPK(customerid)) return null;
			else return retVal;
		}

		/// <summary>
		/// Run the persist for an object
		/// </summary>
		public bool Persist(CustomerDTO dto)
		{
			return (dto.Persist() != null);
		}

		/// <summary>
		/// Run the persist for a list
		/// </summary>
		public bool Persist(List<CustomerDTO> list)
		{
			return list.Persist();
		}

		/// <summary>
		/// Run a selection for all objects
		/// </summary>
		public List<CustomerDTO> RunSelect()
		{
			List<CustomerDTO> retVal = new List<CustomerDTO>();
			retVal.RunSelect();
			return retVal;
		}

		/// <summary>
		/// Run a selection using LINQ
		/// </summary>
		public List<CustomerDTO> RunSelect(string linq)
		{
			List<CustomerDTO> retVal = new List<CustomerDTO>();
			retVal.RunSelect(linq);
			return retVal;
		}

		/// <summary>
		/// Run a paginated selection using LINQ
		/// </summary>
		public PagedQueryResults<CustomerDTO> RunSelect(int pageIndex, int pageSize, bool ascending, string sortColumn, string linq)
		{
			List<CustomerDTO> list = new List<CustomerDTO>();
			string sortOrder;
			if (ascending)
				sortOrder = "ascending";
			else
				sortOrder = "descending";
			return list.RunSelect(pageSize.ToString(), pageIndex.ToString(), sortOrder, sortColumn, linq);
		}

		/// <summary>
		/// Return a list of related CustomerCustomerDemo objects
		/// </summary>
		public List<CustomerCustomerDemoDTO> GetCustomerCustomerDemoList(CustomerDTO item)
		{
			List<CustomerCustomerDemoDTO> retval = new List<CustomerCustomerDemoDTO>();
			retval.SelectByCustomer(item.CustomerId);
			return retval;
		}

		/// <summary>
		/// Return a list of related Order objects
		/// </summary>
		public List<OrderDTO> GetOrderList(CustomerDTO item)
		{
			List<OrderDTO> retval = new List<OrderDTO>();
			retval.SelectByCustomer(item.CustomerId);
			return retval;
		}

		/// <summary>
		/// Return a list of items by the City field
		/// </summary>
		public List<CustomerDTO> SelectByCity(CustomerDTO item, string city)
		{
			List<CustomerDTO> retval = new List<CustomerDTO>();
			retval.SelectByCityField(city);
			return retval;
		}

		/// <summary>
		/// Delete the specified item
		/// </summary>
		public bool Delete(CustomerDTO item)
		{
			Customer customer = Customer.SelectUsingPK(item.CustomerId);
			if (customer == null) return false;
			customer.Delete();
			customer.Persist();
			return true;
		}

		#endregion

	}
}
